package com.hyjx.framework.common.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 操作系统信息工具类
 *
 * @author dijian
 */
public class SystemInfoUtil {
    public static void main(String[] args) throws Exception {
        String ip = getLocalIp();
        System.out.println("IP:" + ip);
    }

    /**
     * 返回本机IP的字符串形式
     *
     * @return
     *
     * @throws Exception
     */
    public static String getLocalIp() {
        InetAddress inet;
        try {
            inet = getSystemLocalIP();
            if (null != inet) {
                String ip = inet.getHostAddress();
                return ip;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取本机ip的InetAddress形式
     *
     * @return
     *
     * @throws Exception
     */
    private static InetAddress getSystemLocalIP() throws Exception {


        InetAddress inet = null;
        String osName = getSystemOsName();
        if ("Windows".compareToIgnoreCase(osName) < 0) {
            inet = getWinLocalIp();
        } else {
            inet = getUnixLocalIp();
        }
        return inet;
    }


    /**
     * 获取类Unix系统的IP
     *
     * @return
     *
     * @throws Exception
     */
    private static InetAddress getUnixLocalIp() throws Exception {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        if (null == netInterfaces) {
            throw new Exception("获取类Unix系统的IP失败");
        }
        InetAddress ip = null;
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = netInterfaces.nextElement();
            if (ni.isUp()) {
                Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
                while (addressEnumeration.hasMoreElements()) {
                    ip = addressEnumeration.nextElement();
                    if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                        return ip;
                    }
                }
            }
        }
        throw new Exception("获取类Unix系统的IP失败");
    }


    /**
     * 获取window系统的ip，貌似不会返回null
     *
     * @return
     *
     * @throws UnknownHostException
     */
    private static InetAddress getWinLocalIp() throws UnknownHostException {
        InetAddress inet = InetAddress.getLocalHost();
        return inet;
    }


    /**
     * 获取操作系统名称
     *
     * @return
     */
    private static String getSystemOsName() {
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name");
        return osName;
    }
}
